home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / VirtualInputDevice / VirtualInputDevice.java.z / VirtualInputDevice.java
Encoding:
Java Source  |  2003-08-08  |  8.8 KB  |  254 lines

  1. /*
  2.  *    @(#)VirtualInputDevice.java 1.13 02/04/01 15:03:56
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import javax.media.j3d.*;
  41. import javax.vecmath.*;
  42. import java.awt.*;
  43. import java.awt.event.*;
  44.  
  45. public class VirtualInputDevice implements InputDevice {
  46.  
  47.     private Vector3f position = new Vector3f();
  48.     private Transform3D newTransform = new Transform3D();
  49.     Sensor sensors[] = new Sensor[1];
  50.  
  51.     // The wheel controls control the view platform orientation
  52.     private RotationControls rotControls;
  53.  
  54.     // The button position controls control the view platform position
  55.     private PositionControls positionControls;
  56.  
  57.     private Transform3D rotTransX = new Transform3D();
  58.     private Transform3D rotTransY = new Transform3D();
  59.     private Transform3D rotTransZ = new Transform3D();
  60.  
  61.     private Vector3f initPos = new Vector3f();
  62.  
  63.     private int processingMode;
  64.     private SensorRead sensorRead = new SensorRead();
  65.  
  66.     // These are the settable parameters.
  67.     private boolean printvalues;
  68.     private int xscreeninitloc;
  69.     private int yscreeninitloc;
  70.     private int xscreensize;
  71.     private int yscreensize;
  72.     private float xobjinitloc;
  73.     private float yobjinitloc;
  74.     private float zobjinitloc;
  75.     private float xaxisrotinit;
  76.     private float yaxisrotinit;
  77.     private float zaxisrotinit;
  78.  
  79.     /* 
  80.      * Create a device, and use the string arguments in args to construct
  81.      * the device with user preferences.
  82.      */
  83.     public VirtualInputDevice( String[] args ) {
  84.  
  85.         // default user-definable values
  86.         printvalues = false;
  87.         xscreeninitloc = 400;
  88.         yscreeninitloc = 0;
  89.         xscreensize = 400;
  90.         yscreensize = 200;
  91.         xobjinitloc = 0.0f;
  92.         yobjinitloc = 0.0f;
  93.         zobjinitloc = 2.2f;
  94.         xaxisrotinit = 0.0f;
  95.         yaxisrotinit = 0.0f;
  96.         zaxisrotinit = 0.0f;
  97.  
  98.  
  99.         for(int i=0 ; i<args.length ; i+=2) {
  100.            if(args[i] == null)
  101.                   break;
  102.            else if(args[i] == "printvalues")
  103.                   printvalues =  (Boolean.valueOf(args[i+1])).booleanValue();
  104.            else if(args[i] == "xscreeninitloc")
  105.                   xscreeninitloc =  (Integer.valueOf(args[i+1])).intValue();
  106.            else if(args[i] == "yscreeninitloc")
  107.                   yscreeninitloc =  (Integer.valueOf(args[i+1])).intValue();
  108.            else if(args[i] == "xscreensize")
  109.                   xscreensize =  (Integer.valueOf(args[i+1])).intValue();
  110.            else if(args[i] == "yscreensize")
  111.                   yscreensize =  (Integer.valueOf(args[i+1])).intValue();
  112.            else if(args[i] == "xobjinitloc")
  113.                   xobjinitloc =  (Float.valueOf(args[i+1])).floatValue();
  114.            else if(args[i] == "yobjinitloc")
  115.                   yobjinitloc =  (Float.valueOf(args[i+1])).floatValue();
  116.            else if(args[i] == "zobjinitloc")
  117.                   zobjinitloc =  (Integer.valueOf(args[i+1])).floatValue();
  118.         }
  119.  
  120.         if(printvalues ==  true) {
  121.            System.out.println("Initial values for VirtualInputDevice:");
  122.            System.out.println("xscreeninitloc = " + xscreeninitloc);
  123.            System.out.println("yscreeninitloc = " + yscreeninitloc);
  124.            System.out.println("xscreeninitsize = " + xscreensize);
  125.            System.out.println("yscreeninitsize = " + yscreensize);
  126.            System.out.println("xobjinitloc = " + xobjinitloc);
  127.            System.out.println("yobjinitloc = " + yobjinitloc);
  128.            System.out.println("zobjinitloc = " + zobjinitloc);
  129.            System.out.println("xaxisrotinit = " + xaxisrotinit);
  130.            System.out.println("yaxisrotinit = " + yaxisrotinit);
  131.            System.out.println("zaxisrotinit = " + zaxisrotinit);
  132.         }
  133.  
  134.  
  135.         // initialize the InputDevice GUI
  136.         Frame deviceFrame = new Frame();
  137.         deviceFrame.setSize(xscreensize,yscreensize);
  138.         deviceFrame.setLocation(xscreeninitloc, yscreeninitloc);
  139.         deviceFrame.setTitle("Virtual Input Device");
  140.         ButtonPositionControls positionControls;
  141.         // initialize position with initial x, y, and z position
  142.         positionControls = new ButtonPositionControls( xobjinitloc, 
  143.                                                   yobjinitloc, zobjinitloc);
  144.         WheelControls rotControls;
  145.         // initialize rotations with initial angles in radians)
  146.         rotControls = new WheelControls(xaxisrotinit, yaxisrotinit,
  147.                                                             zaxisrotinit);
  148.         positionControls.setDevice (this);
  149.         Panel devicePanel = new Panel();
  150.         devicePanel.setLayout( new BorderLayout() );
  151.         devicePanel.add("East", positionControls );
  152.         devicePanel.add("West", rotControls );
  153.         deviceFrame.add( devicePanel );
  154.         deviceFrame.pack();
  155.         deviceFrame.setVisible(true);
  156.  
  157.         initPos.set(xobjinitloc, yobjinitloc, zobjinitloc);
  158.  
  159.         this.positionControls = positionControls;
  160.         this.rotControls = rotControls;
  161.         
  162.         // default processing mode
  163.         processingMode = InputDevice.DEMAND_DRIVEN;
  164.  
  165.     sensors[0] = new Sensor(this);
  166.     }
  167.  
  168.     public void close() {
  169.     }
  170.  
  171.     public int getProcessingMode() {
  172.         return processingMode;
  173.     }
  174.  
  175.     public int getSensorCount() {
  176.     return sensors.length;
  177.     }
  178.  
  179.     public Sensor getSensor( int sensorIndex ) {
  180.     return sensors[sensorIndex];
  181.     }
  182.  
  183.     public boolean initialize() {
  184.     return true;
  185.     }
  186.  
  187.     public void pollAndProcessInput() {
  188.  
  189.         sensorRead.setTime( System.currentTimeMillis() );
  190.  
  191.         rotTransX.rotX(-rotControls.getXAngle());
  192.         rotTransY.rotY(-rotControls.getYAngle());
  193.         rotTransZ.rotZ(-rotControls.getZAngle());
  194.  
  195.         positionControls.getPosition(position);
  196.         newTransform.set(position);
  197.         newTransform.mul( rotTransX );
  198.  
  199.         newTransform.mul(rotTransY);
  200.         newTransform.mul(rotTransZ);
  201.  
  202.         sensorRead.set( newTransform );
  203.         sensors[0].setNextSensorRead( sensorRead );
  204.     }
  205.  
  206.  
  207.     public void processStreamInput() {
  208.     }
  209.  
  210.  
  211.     public void setNominalPositionAndOrientation() {
  212.  
  213.         sensorRead.setTime( System.currentTimeMillis() );
  214.  
  215.         rotTransX.rotX(xaxisrotinit);
  216.         rotTransY.rotY(yaxisrotinit);
  217.         rotTransZ.rotZ(zaxisrotinit);
  218.  
  219.         position.set(initPos);
  220.         
  221.         newTransform.set( position );
  222.  
  223.         newTransform.mul(rotTransX);  
  224.         newTransform.mul(rotTransY);
  225.         newTransform.mul(rotTransZ);
  226.  
  227.         sensorRead.set( newTransform );
  228.         sensors[0].setNextSensorRead( sensorRead );
  229.         rotControls.reset();
  230.         positionControls.setPosition(initPos);
  231.     }
  232.  
  233.  
  234.  
  235.     public void setProcessingMode( int mode ) {
  236.  
  237.          // A typical driver might implement only one of these modes, and
  238.          // throw an exception when there is an attempt to switch modes.
  239.          // However, this example allows one to use any processing mode.
  240.  
  241.          switch(mode) {
  242.             case InputDevice.DEMAND_DRIVEN:
  243.             case InputDevice.NON_BLOCKING:
  244.             case InputDevice.BLOCKING:
  245.                  processingMode = mode;
  246.             break;
  247.             default:
  248.                throw new IllegalArgumentException("Processing mode must " +
  249.                        "be one of DEMAND_DRIVEN, NON_BLOCKING, or BLOCKING");
  250.          }
  251.     }
  252.  
  253. }
  254.